<?php

namespace app\api\model\service;

use think\Model;
class Rebate extends Model
{
    // 表名
    protected $name = 'service_rebate';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';

    public static function getCount($params)
    {
        return self::where($params)->count();
    }

    public static function getNum($params)
    {
        return self::where($params)->sum('num');
    }

    public static function orderRebate($order)
    {
        if(!$order){
            return false;
        }
        if(!$order['skill_id'] && !$order['shop_id'])
        {
            return true;
        }
        $goodsName = OrderDetail::where(['order_id'=>$order['id']])->value('name');
        $skillUserId = Skill::where(['id'=>$order['skill_id']])->value('user_id');
        $shopUserId = $order['shop_id']?Shop::where(['id'=>$order['shop_id']])->value('user_id'):'';
        $rebateData = ['from_user_id'=>$order['user_id'],'type'=>0,'order_id'=>$order['id'],'rebatetype'=>1,'memo'=>$goodsName.'订单收益到账','state'=>1];
        $skillPrice = $rebateData['num'] =$order['skill_id']?self::getRebatePrice($order['skill_percent'],$order['settle_price']):0;
        if($order['shop_id'])
        {
            if($order['skill_id']){
                $rebateData['user_id'] = $skillUserId;
                UserInfo::shopMoney(+$skillPrice,$skillUserId,$goodsName.'门店订单收益到账',$order['id']);
                self::create($rebateData);
                if($order['act_travel_price']>0)
                {
                    UserInfo::shopMoney(+$order['act_travel_price'],$skillUserId,$goodsName.'订单车费结算到账',$order['id']);
                    $rebateData['rebatetype'] = 2;
                    $rebateData['num'] = $order['act_travel_price'];
                    $rebateData['memo'] = $goodsName.'订单车费结算到账';
                    self::create($rebateData);
                    UserInfo::shopUserMoney(+$order['act_travel_price'],$shopUserId,$goodsName.'订单车费结算到账',$order['id']);
                    $rebateData['user_id'] = $shopUserId;
                    $rebateData['type'] = 1;
                    self::create($rebateData);
                }
            }
            $rebateData['user_id'] = $shopUserId;
            $rebateData['type'] = 1;
            $rebateData['rebatetype'] = 0;
            $shopPrice = $rebateData['num'] = self::getRebatePrice($order['shop_percent'],$order['settle_price']);
            UserInfo::shopUserMoney(+$shopPrice,$shopUserId,$goodsName.'订单门店收益到账',$order['id']);
            self::create($rebateData);
        }else{
            $rebateData['rebatetype'] = 0;
            $rebateData['user_id'] = $skillUserId;
            UserInfo::money(+$skillPrice,$skillUserId,$goodsName.'平台订单收益到账',$order['id']);
            self::create($rebateData);
            if($order['act_travel_price']>0)
            {
                UserInfo::money(+$order['act_travel_price'],$skillUserId,$goodsName.'订单车费结算到账',$order['id']);
                $rebateData['rebatetype'] = 2;
                $rebateData['num'] = $order['act_travel_price'];
                $rebateData['memo'] = $goodsName.'订单车费结算到账';
                self::create($rebateData);
            }
        }
        return true;
    }


    public static function getRebatePrice($percent,$settlePrice)
    {
        return bcmul(($percent*$settlePrice),0.01,2);
    }




}
